MIME Types
MIME (Multipurpose Internet Mail Extensions) types tell the client (browser) what kind of content is being sent, so the client knows how to handle or render it.
Format: type/subtype
Examples
| MIME Type | Meaning |
|---|---|
| text/html | HTML page |
| text/css | CSS |
| application/javascript | JavaScript |
| image/png | PNG image |
| application/pdf | PDF file |
Why MIME Types Matter in NGINX
Correct MIME types ensure:
- Proper rendering in browsers
- Correct file downloads
- Security (avoid content sniffing)
- Compatibility with clients and APIs
Wrong MIME types can cause:
- ❌ Files downloading instead of rendering
- ❌ Broken web pages
- ❌ Security warnings
How NGINX Determines MIME Types
NGINX determines the Content-Type header using this order:
- Explicit
typesmapping - Included
mime.typesfile default_typefallback
Core Directives for MIME Types
| Directive | Context | Purpose |
|---|---|---|
| types | http, server, location | Map extensions → MIME types |
| include mime.types | http | Load standard mappings |
| default_type | http, server, location | Fallback MIME type |
Standard MIME Configuration (Recommended)
http {
include mime.types;
default_type application/octet-stream;
}
mime.typescontains hundreds of standard mappingsapplication/octet-streamforces download for unknown files
The mime.types File Explained
Typical Location
/etc/nginx/mime.types
Example Content
types {
text/html html;
text/css css;
application/javascript js;
image/jpeg jpg jpeg;
image/png png;
application/json json;
}
- Multiple extensions can map to one MIME type
- NGINX matches by file extension
MIME Type Resolution Example
Config
root /var/www/html;
Files
/var/www/html/
├── index.html
├── style.css
├── app.js
├── logo.png
├── data.json
Client Receives
| File | Content-Type |
|---|---|
| index.html | text/html |
| style.css | text/css |
| app.js | application/javascript |
| logo.png | image/png |
| data.json | application/json |
default_type Directive
Syntax
default_type mime-type;
Example
default_type text/plain;
- Used when no MIME match exists
- Overrides default behavior if defined in lower context
Overriding MIME Types (Custom Mapping)
Example: Force .log Files to Download
types {
text/plain log;
}
Example: Force File Download
location /downloads/ {
default_type application/octet-stream;
}
MIME Types Per Location
location /media/ {
types {
video/mp4 mp4;
}
}
- Overrides MIME handling only for
/media/
Disabling MIME Type Detection
types { }
default_type application/octet-stream;
- All files downloaded
- Useful for secure file servers
Security Best Practices
Prevent MIME Sniffing
add_header X-Content-Type-Options nosniff;
Prevents browsers from guessing content type.
Correct JavaScript MIME Type
application/javascript js;
Avoids browser console warnings.
Common MIME-Related Issues
| Problem | Cause |
|---|---|
| CSS not loading | Wrong MIME type |
| JS blocked | text/plain instead of JS |
| Images download | Incorrect mapping |
| Browser warnings | Missing nosniff |
Debugging MIME Types
Check Headers
curl -I http://example.com/style.css
Expected:
Content-Type: text/css
Real-World Production Example
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
root /var/www/app;
location / {
try_files $uri $uri/ =404;
}
add_header X-Content-Type-Options nosniff;
}
}
Common Mistakes
- Not including
mime.types - Wrong JS MIME (
text/javascriptin strict setups) - Overriding
default_typeglobally - Relying on browser sniffing